package ACMProble;

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Pro2035 {

	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedInputStream(System.in));
		while(in.hasNext()){
			int A = in.nextInt();
			int B = in.nextInt();
			if(A == 0 && B == 0)
				break;
			int data = A;
			for(int i=1;i<B;i++){
				data = data*A%1000;
			}
			System.out.println(data);
		}
		in.close();
	}

}
